Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import { eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { db, schema } from '@/db' import { getRoomMembers } from '@/lib/arcade/room-membership' import { approveJoinRequest } from '@/lib/arcade/room-join-requests' import { withAuth } from '@/lib/auth/withAuth' import { getUserId } from '@/lib/viewer' import { getSocketIO } from '@/lib/socket-io' /** * POST /api/arcade/rooms/:roomId/join-requests/:requestId/approve * Approve a join request (host only) */ export const POST = withAuth(async (_request, { params }) => { try { const { roomId, requestId } = (await params) as { roomId: string; requestId: string } const userId = await getUserId() // Check if user is the host const members = await getRoomMembers(roomId) const currentMember = members.find((m) => m.userId === userId) if (!currentMember) { return NextResponse.json({ error: 'You are not in this room' }, { status: 403 }) } if (!currentMember.isCreator) { return NextResponse.json( { error: 'Only the host can approve join requests' }, { status: 403 } ) } // Get the request const [request] = await db .select() .from(schema.roomJoinRequests) .where(eq(schema.roomJoinRequests.id, requestId)) .limit(1) if (!request) { return NextResponse.json({ error: 'Join request not found' }, { status: 404 }) } if (request.status !== 'pending') { return NextResponse.json({ error: 'Join request is not pending' }, { status: 400 }) } // Approve the request const approvedRequest = await approveJoinRequest(requestId, userId, currentMember.displayName) // Notify the requesting user via socket const io = await getSocketIO() if (io) { try { io.to(`user:${request.userId}`).emit('join-request-approved', { roomId, requestId, approvedBy: currentMember.displayName, }) console.log( `[Approve Join Request API] Request ${requestId} approved for user ${request.userId} to join room ${roomId}` ) } catch (socketError) { console.error('[Approve Join Request API] Failed to broadcast approval:', socketError) } } return NextResponse.json({ request: approvedRequest }, { status: 200 }) } catch (error: any) { console.error('Failed to approve join request:', error) return NextResponse.json({ error: 'Failed to approve join request' }, { status: 500 }) } }) |